package com.iteye.weimingtom.japcoffee;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

import com.iteye.weimingtom.japcoffee.R;

public class WordActivity extends Activity {
	public final static String EXTRA_ROWID = "EXTRA_ROWID";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.word);
        
        initSearch();

        Uri uri = getIntent().getData();
        Cursor cursor = getWord(this.getIntent().getStringExtra(EXTRA_ROWID));

        if (cursor == null) {
            finish();
        } else {
            cursor.moveToFirst();

            TextView word = (TextView) findViewById(R.id.word);
            TextView definition = (TextView) findViewById(R.id.definition);

            int wIndex = cursor.getColumnIndexOrThrow(DictionaryDatabase.KEY_WORD);
            int dIndex = cursor.getColumnIndexOrThrow(DictionaryDatabase.KEY_DEFINITION);

            word.setText(cursor.getString(wIndex));
            definition.setText(cursor.getString(dIndex));
        }
    }
    
    private DictionaryDatabase mDictionary;
    private void initSearch() {
    	mDictionary = new DictionaryDatabase(this);
    }    
    private Cursor getWord(String rowId) {
        String[] columns = new String[] {
            DictionaryDatabase.KEY_WORD,
            DictionaryDatabase.KEY_DEFINITION};

        return mDictionary.getWord(rowId, columns);
      }
}
